﻿#region File Description
// EpicLeadManager
// EpicLeadManager
// PlayerSQLManager.cs
#endregion

#region Using References
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using EpicLeadManager.Models;

#endregion

namespace EpicLeadManager.Controllers.SQLManager
{
    public sealed class PlayerSqlManager : SqLiteManager
    {
        #region Constructors
        public PlayerSqlManager()
        {
            LoadDatabase<Player>();

            if (Db == null)
                throw new DataException("sqlConnection");
        }
        #endregion

        #region CRUD
        /// <summary>
        ///     Adds the player.
        /// </summary>
        /// <param name="playerToAdd">The player to add.</param>
        public void AddPlayer(Player playerToAdd)
        {
            if (String.IsNullOrEmpty(playerToAdd.Name)) {
                MessageBox.Show("Merci d'ajouter le nom du joueur.");
            }
            else if (playerToAdd.Profession == new Profession()) {
                MessageBox.Show("Merci d'ajouter la classe du joueur.");
            }
            else {
                int p = Db.Insert(playerToAdd);
                Console.WriteLine(@"New player added in database : {0}", p);
            }
        }

        /// <summary>
        ///     Reads the player.
        /// </summary>
        /// <param name="playerName">Name of the player.</param>
        /// <returns></returns>
        public Player ReadPlayer(string playerName)
        {
            Player player = Db.Table<Player>().FirstOrDefault(p => p.Name == playerName);

            return player ?? new Player();
        }

        /// <summary>
        /// Reads the player.
        /// </summary>
        /// <param name="readPlayer">The read player.</param>
        /// <returns></returns>
        public Player LastPlayer()
        {
            Player player = Db.Table<Player>().LastOrDefault();

            return player ?? new Player();
        }


        /// <summary>
        ///     Reads all players.
        /// </summary>
        /// <returns></returns>
        public List<Player> ReadAllPlayers()
        {
            return Db.Table<Player>().ToList();
        }

        /// <summary>
        ///     Updates the player.
        /// </summary>
        /// <param name="playerToUpdate">The player to update.</param>
        public void UpdatePlayer(Player playerToUpdate)
        {
            if (String.IsNullOrEmpty(playerToUpdate.Name)) {
                MessageBox.Show("Merci d'ajouter le nom du joueur.");
            }
            else if (playerToUpdate.Profession == new Profession()) {
                MessageBox.Show("Merci d'ajouter la classe du joueur.");
            }
            else {
                Db.Update(playerToUpdate);
            }
        }

        /// <summary>
        ///     Deletes the player.
        /// </summary>
        /// <param name="playerToDelete">The player to delete.</param>
        public void DeletePlayer(Player playerToDelete)
        {
            Db.Delete(playerToDelete);
        }
        #endregion
    }
}